package com.tdtxdcxm.ruralcommunity.slice;


import com.tdtxdcxm.ruralcommunity.ResourceTable;
import com.tdtxdcxm.ruralcommunity.appruntime.DataCenterControl;
import com.tdtxdcxm.ruralcommunity.appruntime.UserConfigInfo;
import com.tdtxdcxm.ruralcommunity.myutils.MyTools;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;

public class LoginSlice extends AbilitySlice implements Component.ClickedListener{
    private String[][] userinfodown = new String[5][2];
    public static Boolean toanotherslice = false;

    private TextField log_usernametfd,log_passwordtfd;
    private Button submitlogin,log_lbackbt;
    private Text log_tstext;

    private String[] userInfo = new String[2];

    private void initLogComponent(){
        log_usernametfd = (TextField) findComponentById(ResourceTable.Id_log_usernametfd);
        log_passwordtfd = (TextField) findComponentById(ResourceTable.Id_log_passwordtfd);
        submitlogin = (Button) findComponentById(ResourceTable.Id_submitlogin);
        log_lbackbt = (Button) findComponentById(ResourceTable.Id_log_lbackbt);
        log_tstext = (Text) findComponentById(ResourceTable.Id_log_tstext);

        submitlogin.setClickedListener(this);
        log_lbackbt.setClickedListener(this);

        log_passwordtfd.addTextObserver(new Text.TextObserver() {
            @Override
            public void onTextUpdated(String s, int i, int i1, int i2) {
                if(!s.equals("") && !s.matches("^[0-9]+$")){
                    log_tstext.setText("只能是数字");
                    log_passwordtfd.setText(s.substring(0,s.length() - 1));
                }
            }
        });
    }

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_loginpage);

        initLogComponent();
    }

    @Override
    public void onActive() {
        super.onActive();
        log_tstext.setText("您好！");
    }

    @Override
    protected void onInactive() {
        super.onInactive();
        if(toanotherslice){
            terminate();
        }

    }

    @Override
    protected void onBackground() {
        super.onBackground();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onStop() {
        super.onStop();
    }


    @Override
    public void onClick(Component component) {
        if(component == submitlogin){
            if(log_usernametfd.getText().equals("")){
                log_tstext.setText("账号不能为空！");
                return;
            }
            if(log_passwordtfd.getText().equals("")){
                log_tstext.setText("密码不能为空！");
                return;
            }
            if(log_passwordtfd.getText().length() > 8){
                log_tstext.setText("密码不能超过8位！");
                return;
            }
            log_tstext.setText("正在登录中......");

            String inputname = log_usernametfd.getText();
            String inputpassword = log_passwordtfd.getText();
            userInfo[0] = inputname;
            userInfo[1] = inputpassword;

            if(UserConfigInfo.compareData(this, inputname, "username", inputname)){
                if(UserConfigInfo.compareData(this, inputname, "password", inputpassword)){
                    log_usernametfd.setText("");
                    log_passwordtfd.setText("");
                    log_tstext.setText("本地验证：密码正确！");

                    DataCenterControl.userName = userInfo[0];
                    UserConfigInfo.pullData(this.getContext(),userInfo);

                    present(new MainAbilitySlice(),new Intent());

                    toanotherslice = true;
                }
                else{
                    log_tstext.setText("本地验证：密码错误！");
                    return;
                }
            }
            else{
                log_tstext.setText("本地验证：未从该机注册！远程验证...");
                MyTools.UpDataLoginInfo(LoginSlice.this
                                        ,userInfo
                                        ,userinfodown
                                        ,log_tstext
                                        ,log_usernametfd
                                        ,log_passwordtfd);
            }
        }
        if(component == log_lbackbt)
        {
            log_usernametfd.setText("");
            log_passwordtfd.setText("");
            present(new StartPage(),new Intent());
            toanotherslice = true;
        }
    }
}
